#include "../../include/threadPool.h"
#include "../../include/worker.h"


void cleanFunc(void *arg){
    threadPool_t * pThreadPool = (threadPool_t *)arg;
    pthread_mutex_unlock(&pThreadPool->taskQueue.mutex);
}

void * handleEvent(void *arg){
    threadPool_t * pThreadPool = (threadPool_t *)arg;
    int netFd;
    event_t evt;
    while(1){
        int flag = 0;
        printf("I am free! tid = %lu\n", pthread_self());
        pthread_mutex_lock(&pThreadPool->taskQueue.mutex);
        pthread_cleanup_push(cleanFunc,(void *)pThreadPool);
        while(pThreadPool->taskQueue.size == 0 && pThreadPool->exitFlag == 0){
            pthread_cond_wait(&pThreadPool->taskQueue.cond,&pThreadPool->taskQueue.mutex);
        }
        if(pThreadPool->exitFlag != 0){ //在进行任务之前先检测主进程是否要终止，如果要就退出
            printf("I am going to die child thread!\n");
            pthread_exit(NULL);
        }
        //现在子线程苏醒了
        netFd = pThreadPool->taskQueue.pFront->netFd;//拿到了队首的文件描述符
        taskDequeue(&pThreadPool->taskQueue);
        pthread_cleanup_pop(1);
        bzero(&evt,sizeof(evt));
        sql_init(&evt.mysql, pThreadPool->passwd);
        usrdata_t usr;
        evt.usrdata = &usr;
        evt.netFd = netFd;
        printf("I am working! tid = %lu\n", pthread_self());
        
        while(1) {
            puts("start");
            if(flag == 0)
            {
                flag = 1;
                int ret = start(&evt);
                if(ret == -1) {
                    break;
                }
            }
            int ret = smsgdef(&evt);
            if(ret == -1)
                break;
            printf("done\n");
        }
        mysql_close(evt.mysql);
        close(evt.netFd);
    }
}

int makeWorker(threadPool_t *pThreadPool){
    for(int i = 0; i < pThreadPool->threadNum; ++i){
        pthread_create(&pThreadPool->tid[i],NULL,handleEvent,(void *)pThreadPool);
    }
}

int smsgdef(event_t * evt) {
    int netFd = evt->netFd;
    int cmd;
    int ret;
    printf("ready to recv cmd!\n");
    ret = recvn(netFd, &cmd, sizeof(cmd));
    THREAD_ERROR_CHECK(ret, "recv");
    train_t train;
    switch(cmd) {
        case 0:     //ls命令
            myls(train.buf);
            train.length = strlen(train.buf);
            send(netFd, &train, sizeof(train.length)+train.length, MSG_NOSIGNAL);
            break;
        case 1:     //pwd命令
            getcwd(train.buf, sizeof(train.buf));
            train.length = strlen(train.buf);
            send(netFd, &train, sizeof(train.length)+train.length, MSG_NOSIGNAL);
            break;
        case 2:     //exit命令
            puts("client want to close.");
            return -1;
            break;
        case 3:
            msgprocess(netFd, &train);
            ret = chdir(train.buf);
            break;
        case 4:

            break;
        case 5:

            break;
        case 6:
            msgprocess(netFd, &train);
            myrmfile(train.buf);
            break;
        case 7:
            msgprocess(netFd, &train);
            mymkdir(evt, train.buf);
            break;
    }
    return 0;
}